

function need_send_email_social() {
    var a = document.getElementById('a');
    var b = document.getElementById('b');
    a.style.display = 'none';
    b.style.display = 'block';
    document.getElementById('need_send_mail_value').innerText = 'true';
}

function cancel_send_email_social() {
    var a = document.getElementById('a');
    var b = document.getElementById('b');
    a.style.display = 'block';
    b.style.display = 'none';
    document.getElementById('need_send_mail_value').innerText = 'false';
}

// 设置cookie
function setCookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}

// 获取cookie
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


function getHeadUrl() {
    var url = window.location.href;
    var head_url = '';
    if (url.startsWith('https://')) {
        head_url = url.substring(0, url.indexOf('/', 9));
    } else if (url.startsWith('http://')) {
        head_url = url.substring(0, url.indexOf('/', 8));
    } else {
        head_url = url.substring(0, url.indexOf('/', 1));
    }
    return head_url;
}


function social_attack(attack_type) {
    var is_mail = document.getElementById('need_send_mail_value').innerText;
    console.log(is_mail)
    if (is_mail == 'true') {
        console.log('发送邮件的')
        is_mail = true;
        } else {
        is_mail = false;
    }
    console.log(is_mail);
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/social_attack', true);
    xhr.send(JSON.stringify({
        'type': attack_type , 
        'send_mail' : is_mail,
        "token": getCookie('seeion_code')
    }));
    //xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function() {
        if (xhr.responseText == '') {
            return;
        }
        var response = JSON.parse(xhr.responseText);
        if (response['status'] == 'ok'){
            showAlert('生成钓鱼链接 '+ getHeadUrl() + "/" + response['result']);
        }
        else {
            showAlert('调用接口失败' + response['result']);
            return;
        }
        // 默认使用长轮训通讯
        var socket_xhr = new XMLHttpRequest();
        socket_xhr.open('POST', '/socket', true);
        socket_xhr.onreadystatechange = function() {
            if (socket_xhr.readyState == 4) {
                if (socket_xhr.status == 200) {
                    if (socket_xhr.responseText == '') {
                        return;
                    }
                    console.log(socket_xhr.responseText);
                    var response = JSON.parse(socket_xhr.responseText);
                    showAlert(response['result']);
                } else {
                    showAlert('通讯连接中断');
                }
            }
        }
        socket_xhr.send(JSON.stringify({
            'token' : response['result'],
            'is_long_socket' : true
        }));
    }
}